package cn.xfdzx.android.apps.shop.util;
 
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
 * @author Chen.Zhidong
 *
 */
public class StreamUtil {
    /**
     * InputStream转String
     * 这里用的方法和下面一个函数方法不同，看官看到下面的函数自然会明白
     * @param is
     * @param encode
     * @return
     */
    public static String stream2string(InputStream is, String encode) {
        if (is != null) {
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, encode));
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                return sb.toString();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }
 
    /**
     * InputStream转jsonarray
     * 此处在处理InputStream时所用的方法和上面一个函数不同，只是多列出一个可用方法，实际使用时，请考虑一下效率问题
     * 至于转成jsonarray的笨办法只是由于个人需求，接口也是自己写的，有十足把握不会导致出错，各位看官莫怪，轻易不要这样干
     * @param is
     * @return
     */
    public static String stream2jarray(InputStream is) {
        if (is != null) {
            StringBuffer sb = new StringBuffer();
            byte[] b = new byte[4096];
            try {
                for(int n;(n=is.read(b))!=-1;){
                    sb.append(new String(b,0,n));
                }
                is.close();
                if(sb.charAt(0) == '{'){
                    return "["+sb.toString()+"]";
                }
                else{
                    return sb.toString();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }
 
    /**
     * InputStream转jsonobject
     * 转jsonobject时同样使用了笨办法，轻易不要照我这样用
     * @param is
     * @return
     */
    public static String stream2jobject(InputStream is) {
        if (is != null) {
            StringBuffer sb = new StringBuffer();
            byte[] b = new byte[4096];
            try {
                for(int n;(n=is.read(b))!=-1;){
                    sb.append(new String(b,0,n));
                }
                is.close();
                if (sb.charAt(0) == '[') {
                    return "{\"jarray\":" + sb.toString() + "}";
                }
                else {
                    return sb.toString();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }
 
    public static String stream2string(InputStream is) {
        return stream2string(is, "utf-8");
    }
 
    /**
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] stream2byte(InputStream inStream) throws Exception {
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inStream.close();
        return data;
    }
}